dojo.require("esri.map");

dojo.addOnLoad(init);

var map,
  	travelGraphicsLayer,
  	travellerName;
var layersLoaded = 0;

function init() {
	$(document).ready(jQueryReady);
}

function initMap() {
	mapLoading = dojo.byId("loading_map");

	map = createTravellingAnimalsMap();
	
	travelGraphicsLayer = new esri.layers.GraphicsLayer();
    map.addLayer(travelGraphicsLayer);
}

function jQueryReady() {
	$("#loading_way").show();
	
	var language = navigator.language || navigator.systemLanguage || navigator.userLanguage || 'en';
	autolocalize();
	
	$("button").button();
	$("#local_leger").text(LOCAL.leger);
	$("#local_aerien").text(LOCAL.aerien);
	$("#local_plan").text(LOCAL.plan);
	$("#local_mixte").text(LOCAL.mixte);
	
	$("#div_carte").show("fade", {}, 500);
	
	initMap();	
	map.resize(true);
	map.reposition();
	
	travellerName = "kevin";
	
	$.ajax({
		type: "GET",
		data: {name: travellerName},
		url: "gettravellerinfos",
		success: onGetTravellerInfos
		});

}

function onGetTravellerInfos(data) {
	var traveller = JSON.parse(data);

		travellerName = traveller.name;	
		completeName = traveller.completeName;
		
		var first = true;
		var multipoint = newMultipoint();
		for(var n in traveller.positions) {
			var x = traveller.positions[n].x,
				y = traveller.positions[n].y;
			var point = newPoint(x, y);
			multipoint.addPoint(point);
			travelGraphicsLayer.add(getPositionGraphic(x, y, first));
			if(!first) {
				travelGraphicsLayer.add(getLineGraphic(previousx, previousy, x, y));
			}
			first = false;
			previousx = x;
			previousy = y;
		}
		
		zoomToPoints(map, multipoint);
		
		$("#loading_way").hide();
}

function onAdresseComplete(candidate) {

}


